package com.controller;

import com.form.examination.AddExaminationFrom;
import com.form.examination.ExamPageForm;
import com.form.examination.ExamRecordPageForm;
import com.form.examination.SubmitExamForm;
import com.service.ExaminationPaperService;
import com.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Version: V1.0    <br/>
 * Datetime:   2022/5/15 23:25   <br/>
 * Description: 试卷控制器
 *
 * @author: chen
 */
@RestController
@RequestMapping(value = "/examination")
public class ExaminationController {

    @Autowired
    private ExaminationPaperService examinationPaperService;

    /**
     * 添加试卷
     */
    @PostMapping(value = "/add")
    public Msg addExamination(@RequestBody AddExaminationFrom addExaminationFrom) {
        addExaminationFrom.setId(null);
        return examinationPaperService.createExaminationPaper(addExaminationFrom) ? Msg.success() : Msg.error();
    }

    /**
     * 删除试卷
     */
    @PostMapping(value = "/delete")
    public Msg deleteById(Integer id) {
        if (id == null) {
            return Msg.error("id不能为空");
        }
        return examinationPaperService.deleteById(id) ? Msg.success() : Msg.error();
    }

    /**
     * 更新试卷
     */
    @PostMapping(value = "/update")
    public Msg updateExamination(@RequestBody AddExaminationFrom addExaminationFrom) {
        if (addExaminationFrom.getId() == null) {
            return Msg.error("id不能为空");
        }
        return examinationPaperService.updateExaminationPaper(addExaminationFrom) ? Msg.success() : Msg.error();
    }

    /**
     * 查询试卷
     */
    @GetMapping(value = "/query")
    public Msg getById(Integer id) {
        if (id == null) {
            return Msg.error("id不能为空");
        }
        return Msg.success(examinationPaperService.getById(id));
    }

    /**
     * 查询试卷
     */
    @GetMapping(value = "/pageList")
    public Msg pageList(ExamPageForm examPageForm) {
        return Msg.success(examinationPaperService.pageList(examPageForm));
    }

    /**
     * 进入考试
     *
     * @param userId  用户id
     * @param paperId 试卷id
     * @return 返回考试信息
     */
    @GetMapping(value = "/requestExam")
    public Msg entranceExam(Integer userId, Integer paperId, Boolean confirm) {
        if (userId == null || paperId == null) {
            return Msg.error("参数不能为空");
        }
        if (confirm == null) {
            confirm = false;
        }
        return examinationPaperService.entranceExam(userId, paperId, confirm);
    }

    @GetMapping("/getExamInfo")
    public Msg getExaminationContent(Integer paperId) {
        if (paperId == null) {
            return Msg.error("参数不能为空");
        }
        return examinationPaperService.getExaminationContent(paperId);
    }

    @PostMapping("/submitExam")
    public Msg submitTheTestPaper(@RequestBody SubmitExamForm submitExamForm) {
        if (submitExamForm.getPaperId() == null || submitExamForm.getUserId() == null) {
            return Msg.error("参数不能为空");
        }
        return examinationPaperService.submitTheTestPaper(submitExamForm);
    }

    /**
     * 分页获取考试记录
     */
    @GetMapping("/pageExamRecord")
    public Msg getExamPageRecords(ExamRecordPageForm pageForm) {
        if (pageForm == null || pageForm.getUserId() == null) {
            return Msg.error("参数不能为空");
        }
        return Msg.success(examinationPaperService.getExamPageRecords(pageForm));
    }

    @GetMapping("/getExamRecordDetail")
    public Msg getExamRecordDetails(Integer recordId) {
        if (recordId == null) {
            return Msg.error("参数不能为空");
        }
        return examinationPaperService.getExamRecordDetails(recordId) ;
    }
}